const utils = require('../../../../common/util/util')

Page({
    data: {
        list : [] ,
        act : -1
    },

    onLoad: function () {
        this.onShow()
        this.setData({
            act : -1
        })
    },

    onShow: function () {
        this.xInit()
        this.setData({
            act : -1
        })
    },
    
    xInit : function () {
        let that = this
        wx.cloud.callFunction({
            name: 'dynamic_content',
            data: {
                action: "my_dynamic_content",
            }
        }).then(res => {
            console.log("我的帖子列表:", res)
            let data = res.result
            for(var i in data){
                data[i].release_time = utils.formatTime_emotion(new Date(data[i].release_time))
            }
            data = data.reverse()
            that.setData({
                list : data
            })
        })
    },


    xLike : function (e) {
        let that = this
        let id = that.data.id
        let params = e.currentTarget.dataset
        wx.cloud.callFunction({
            name: 'dynamic_content',
            data: {
                action : "thumbs_up",
               _id: params.id ,
            }
        }).then( res => {
            that.setData({
                [`list[${params.index}].me_thumbs_up`] : !params.state ,
                [`list[${params.index}].total_thumbs_up`] : !params.state ? params.key + 1 : params.key - 1
            })
            if(!params.state){
                wx.showToast({
                    title: '点赞成功',
                    icon: 'none'
                })
            }else{
                wx.showToast({
                    title: '取消成功',
                    icon: 'none'
                })
            }
        })
    },

    xToDetail : function (e) {
        let that = this
        that.setData({
            act : -1
        })
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '/common/pages/posts/detail/index?id=' + id,
        })
    },

    xToMore : function (e) {
        let that = this
        let key = e.currentTarget.dataset.key
        let act = that.data.act
        if(act === key){
            that.setData({
                act : -1 ,
            })
        }else{
            that.setData({
                act : key ,
            })
        }
        
        
    },

    xDelete : function (e) {
        let that = this
        let id = e.currentTarget.dataset.id
        let data = {
            is_delete: 1
        }
        console.log(id)
        wx.showModal({
            title : '您确定删除这个帖子吗？' ,
            success: function(res) {
                if (res.confirm) {
                    wx.cloud.callFunction({
                        name: 'dynamic_content',
                        data: {
                            action : "update_dynamic_content",
                            _id : id,
                            dataObj : data
                        }
                    }).then( () => {
                        that.setData({
                            act : -1
                        })
                        that.xInit()
                    })
                } else if (res.cancel) {
                    that.setData({
                        act : -1
                    })
                }
            }
        })
    },

    xToCreate : function () {
        wx.navigateTo({
            url: '/common/pages/posts/index/index',
        })
    }
})